package com.gopay.cashier.web.controller;

import com.gopay.cashier.common.exception.WebException;
import com.gopay.cashier.service.SmsSupportService;
import com.gopay.common.domain.cps.CpsGenMainOrder;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 14-8-21.
 */
@Controller
public class SmsSupportController extends BaseController{

    @Resource(name = "smsSupportService")
    private SmsSupportService smsSupportService;

    private static int sendMaxCount = 5;//一天可以获取5次

    private static int failMaxCount = 3;//一天可以错误3次
    //生产优化14期 手机号只判断以1开头
    private static Pattern p = Pattern.compile("^1\\d{10}$");

    @RequestMapping("/sms/send.shtml")
    @ResponseBody
    public String sendSMS(String bankPhoneNo){
        JSONObject json = new JSONObject();
        if(!p.matcher(bankPhoneNo).matches()){
            json.accumulate(FIELD_ERR_MSG,"手机号码格式错误");
            json.accumulate(FIELD_ERR_CODE,"1");
            return json.toString();
        }
        String smsContent = null;
        try {
            CpsGenMainOrder order = getMainOrder(getOrderId(), true);
            smsContent ="国付宝快捷支付验证码：%s，支付金额："+ new DecimalFormat("0.00").format(order.getMerTxnAmt())+"元。请勿将验证码告知他人并确认支付网址为合法网址。";
        } catch (WebException e) {
            e.printErrMsg();
            json.accumulate(FIELD_ERR_MSG, "订单不存在，请求失败");
            json.accumulate(FIELD_ERR_CODE,"1");
            return json.toString();
        }

        int failCount = smsSupportService.checkFailSmsCount(bankPhoneNo);
        if((failCount > failMaxCount-1)){
            json.accumulate(FIELD_ERR_MSG, "您的短信验证码已连续输错三次，为了保障您的支付安全，该手机号已被冻结，隔日解冻。");
            json.accumulate(FIELD_ERR_CODE,"1");
            return json.toString();
        }

        boolean canSend = smsSupportService.checkSendSmsCount(bankPhoneNo, sendMaxCount);
        if(!canSend){
            json.accumulate(FIELD_ERR_MSG,"该手机号获取短信验证码次数超限");
            json.accumulate(FIELD_ERR_CODE,"1");
            return json.toString();
        }

        boolean res = smsSupportService.sendSMS(bankPhoneNo,smsContent,true);
        if(res){
            json.accumulate(FIELD_ERR_MSG,"短信已发送到 "+bankPhoneNo+" 请查收");
            json.accumulate(FIELD_ERR_CODE,"0");
            return json.toString();
        }else{
            json.accumulate(FIELD_ERR_MSG,"获取短信验证码失败，请稍后再试");
            json.accumulate(FIELD_ERR_CODE,"1");
            return json.toString();
        }
    }

    @RequestMapping("/sms/check.shtml")
    @ResponseBody
    public String checkSMS(String bankPhoneNo,String verifyCode){
        JSONObject json = new JSONObject();
        if(!p.matcher(bankPhoneNo).matches()){
            json.accumulate(FIELD_ERR_MSG,"手机号码格式错误");
            json.accumulate(FIELD_ERR_CODE,"1");
            return json.toString();
        }
        if(StringUtils.isBlank(verifyCode)){
            json.accumulate(FIELD_ERR_MSG,"手机验证码不能为空");
            json.accumulate(FIELD_ERR_CODE,"1");
            return json.toString();
        }

        if(!p.matcher(bankPhoneNo).matches()){
            json.accumulate(FIELD_ERR_MSG,"手机号码格式错误");
            json.accumulate(FIELD_ERR_CODE,"1");
            return json.toString();
        }

        int failCount = smsSupportService.checkFailSmsCount(bankPhoneNo);
        if((failCount > failMaxCount-1)){
            json.accumulate(FIELD_ERR_MSG,"您的短信验证码已连续输错三次，为了保障您的支付安全，该手机号已被冻结，隔日解冻。");
            json.accumulate(FIELD_ERR_CODE,"1");
            return json.toString();
        }

        boolean checkRes = smsSupportService.checkSMS(bankPhoneNo,verifyCode);
        if(checkRes){
            json.accumulate(FIELD_ERR_MSG,"短信验证码输入正确");
            json.accumulate(FIELD_ERR_CODE,"0");
            return json.toString();
        }else{
            String errorMsg = "短信验证码有误，请您核对后重新输入 </br>温馨提示：连续输错3次，该手机将被冻结，隔日解冻";
            json.accumulate(FIELD_ERR_MSG,errorMsg);
            json.accumulate(FIELD_ERR_CODE,"1");
            json.accumulate("action","1");
            return json.toString();
        }
    }
}
